﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VBFOOL.MediaPlayer.Core.Interface;
using FlacBox;
using System.Windows.Media;
namespace VBFOOL.MediaPlayer.Codecs
{
    public class FlacCodec : ICodecFilter
    {
        public IDictionary<System.Windows.Media.MediaStreamAttributeKeys, string> GetCodecPrivateData(IDictionary<string, object> RawData)
        {
            Dictionary<MediaStreamAttributeKeys, string> att = new Dictionary<MediaStreamAttributeKeys, string>();
            var chn = (int)RawData["Channels"];
            var feq = (decimal)RawData["SamplingFrequency"];
            var bps = (int)RawData["BitDepth"];
            int BlockSize = bps / 8 * chn;
            byte[] b = RawData["CodecPrivateData"] as byte[];
            FlacHeaderReader header = new FlacHeaderReader(new System.IO.MemoryStream(b), true);
            while (header.Read())
            {

            }
            info = header.Streaminfo;
            //info.BitsPerSample = 16;
            using (var ms = new System.IO.MemoryStream())
            {
                var w = new System.IO.BinaryWriter(ms);
                w.Write(new byte[] { 0x01, 0x00 });
                w.Write((short)info.ChannelsCount);
                w.Write((int)info.SampleRate);
                w.Write((int)(info.ChannelsCount * info.SampleRate * info.BitsPerSample / 8));
                w.Write((short)BlockSize);
                w.Write((short)info.BitsPerSample);
                w.Write((short)0);
                byte[] hd = ms.ToArray();
                att[MediaStreamAttributeKeys.CodecPrivateData] = BitConverter.ToString(hd).Replace("-", "");
                return att;
            }
        }

        public bool SupportCodecID(string CodecID)
        {
            return CodecID.Trim() == @"A_FLAC";
        }
        FlacStreaminfo info = null;
        List<string> Logs = new List<string>();

        public byte[] Translate(byte[] source, int offset, int count)
        {
            using (System.IO.MemoryStream src = new System.IO.MemoryStream(source, offset, count))
            {
                DateTime dt = DateTime.Now;
                src.Position = 0;
                WaveOverFlacStream flacStream = new WaveOverFlacStream(info, src);
                var ms2 = new System.IO.MemoryStream();
                byte[] b = new byte[16384];
                while (true)
                {
                    int i = flacStream.Read(b, 0, b.Length);
                    if (i > 0)
                    {
                        ms2.Write(b, 0, i);
                    }
                    else
                    {
                        Logs.Add(string.Format("Use Time:{0}", (DateTime.Now - dt).TotalMilliseconds));
                        ms2.Position = 0;
                        return ms2.ToArray();
                    }
                }
            }
        }
    }
}
